package com.nimitz.modules.approval.controller;


import com.nimitz.common.core.constant.UserConstants;
import com.nimitz.common.core.web.controller.BaseController;
import com.nimitz.common.core.web.domain.AjaxResult;
import com.nimitz.modules.approval.domain.ApprovalNodeReview;
import com.nimitz.modules.approval.domain.ApprovalProcedure;
import com.nimitz.modules.approval.domain.ApprovalProcedureNode;
import com.nimitz.modules.approval.service.IApprovalProcedureNodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ApprovalProcedureNodeController
 * @Author FuCun
 * @Date 2021-08-17-14:45
 * @PackageName com.nimitz.modules.approval.controller
 * @Description 审批流节点明细
 * @Version 1.0.0
 */
@RestController
@Api(tags = "审批流节点明细管理")
@RequestMapping("/approval-procedure-node")
public class ApprovalProcedureNodeController extends BaseController {
    @Autowired
    private IApprovalProcedureNodeService approvalProcedureNodeService;

    /**
     * 查询所有审批流节点明细（含搜索）
     *
     * @param  approvalProcedureNode 审批流节点明细
     * @return 审批流节点明细集合
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有审批流节点明细（含搜索）")
    public AjaxResult list(ApprovalProcedureNode approvalProcedureNode){
        List<ApprovalProcedureNode> approvalProcedureNodes=approvalProcedureNodeService.selectApprovalProcedureNodeList(approvalProcedureNode);
        return AjaxResult.success(approvalProcedureNodes);
    }


    /**
     * 新增审批流节点明细信息
     *
     * @param approvalProcedureNode 审批流节点明细
     * @return 结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增审批流节点明细信息")
    public AjaxResult add(@RequestBody ApprovalProcedureNode approvalProcedureNode) {
        if (UserConstants.NOT_UNIQUE.equals(approvalProcedureNodeService.checkApprovalProcedureNodeNameUnique(approvalProcedureNode.getProcedureNodeName()))) {
            return AjaxResult.error("新增审批流节点明细信息'" + approvalProcedureNode.getProcedureNodeName() + "'失败，审批流节点名称已存在");
        }
        //该处的创建者应该为登录用户的名称，暂时写死在这里。
        approvalProcedureNode.setProcedureNodeName("admin");
        return toAjax(approvalProcedureNodeService.insertApprovalProcedureNode(approvalProcedureNode));
    }

    /**
     * 修改审批流节点明细信息
     *
     * @param approvalProcedureNode 审批流节点明细
     * @return 结果
     */
    @PutMapping("/edit")
    @ApiOperation(value = "修改审批流节点明细信息")
    public AjaxResult edit(@RequestBody ApprovalProcedureNode approvalProcedureNode){
        if (UserConstants.NOT_UNIQUE.equals(approvalProcedureNodeService.checkApprovalProcedureNodeNameUnique(approvalProcedureNode.getProcedureNodeName()))) {
            return AjaxResult.error("修改审批流节点明细信息'" + approvalProcedureNode.getProcedureNodeName() + "'失败，审批流节点名称已存在");
        }
        approvalProcedureNode.setProcedureNodeName("admin");
        return toAjax(approvalProcedureNodeService.updateApprovalProcedureNode(approvalProcedureNode));
    }

    /**
     * 删除审批流节点明细信息
     *
     * @param procedureNodeId 流节点明细id
     * @return 结果
     */
    @DeleteMapping("/{procedureNodeId}")
    @ApiOperation(value = "删除审批意见信息")
    public AjaxResult remove(@PathVariable Long procedureNodeId) {
        return toAjax(approvalProcedureNodeService.deleteApprovalProcedureNodeByProcedureId(procedureNodeId));
    }
}
